package cn.itcast.bos.web.action.base;

import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.support.CustomSQLErrorCodesTranslation;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {
	// 模型构造已经完成,在父类中构造

	@Autowired
	private FixedAreaService fixedAreaService;

	@Action(value = "fixArea_save", results = {
			@Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String save() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	@Action(value = "fixedArea_queryPage", results = { @Result(name = "success", type = "json") })
	public String queryPage() {
		// 构造pageable对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 构造Pre对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				// 判断id
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					predicate = cb.and(p1);
				}
				// 判断compony单位
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("compony").as(String.class), "%" + model.getCompany() + "%");
					predicate = cb.and(p2);
				}
				return predicate;
			}
		};
		Page<FixedArea> page = fixedAreaService.findAll(specification, pageable);
		pushToValueStack(page);
		return SUCCESS;
	}

	// 查询未关联定区的列表
	@Action(value = "fixArea_findNoAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findNoAssociationCustomers() {
		// 使用webclient调用webservice接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 查询未关联定区的列表
	@Action(value = "fixArea_findHasAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findHasAssociationCustomers() {
		// 使用webclient调用webservice接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	//属性注入,获得表单提交的具有name属性的值
	private String[] customerIds;
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	//关联客户的操作
	@Action(value="fixedArea_associationCustomersToFixedArea",results={
			@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")
	})
	public String associationCustomersToFixedArea(){
		
		//将得到的id的数组拼接成一个String的字符串
		String customerIdStr = StringUtils.join(customerIds, ",");
		//fixedAreaId封装到Model中
		WebClient.create("http://localhost:9002/crm_management/services/customerService"
				+ "/associationcustomerstofixedarea?customerIdStr="
		+customerIdStr+"&fixedAreaId="+model.getId())
		.put(null);
		return SUCCESS;
	}
	
	//提供属性驱动
	private Integer courierId;
	private Integer takeTimeId;
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}
	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="fixedArea_associationCourierToFixedArea",results={
			@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")
	})
	public String associationCourierToFixedArea(){
		//调用业务层,定区关联快递员
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}

}
